/*
 * A class that defines a test and it's attributes,
 * also keeps track on which the currentPosition and currentQuestion is.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chalmers.se.tda367.model;

import edu.chalmers.se.tda367.observer.GlobalObserverList;
import java.util.ArrayList;
import java.util.List;
import edu.chalmers.se.tda367.question.IQuestion;

/**
 * @author bgrn
 */
public class Test {

    private static final Test INSTANCE = new Test();

    public static Test getInstance() {
        return INSTANCE;
    }
    private int currentPosition;

    public int getCurrentPosition() {
        return currentPosition;
    }
    Test currentTest;
    private IQuestion currentQuestion;
    private List<IQuestion> questions = new ArrayList<IQuestion>(20);

    public IQuestion getCurrentQuestion() {
        return currentQuestion;
    }

    public void setCurrentQuestion(IQuestion currentQuestion) {
        this.currentQuestion = currentQuestion;
    }

    public void startTest(Test t) {
        currentTest = t;
        currentQuestion = t.getQuestions().get(0);
        currentPosition = 0;
        GlobalObserverList.getInstance().firePropertyChange("Update question", null, currentTest);
    }

    public void setQuestions(List<IQuestion> questions) {
        this.questions = questions;
    }

    public List<IQuestion> getQuestions() {
        return questions;
    }

    public void setQuestion(int i) {
        currentPosition = i - 1;
        INSTANCE.setCurrentQuestion(INSTANCE.questions.get(currentPosition));
        GlobalObserverList.getInstance().firePropertyChange("Update question", currentPosition, INSTANCE);
    }

    public void nextQuestion() {
        currentPosition = currentPosition + 1;
        if (currentPosition + 1 == 21) {
            GlobalObserverList.getInstance().firePropertyChange("Test over", null, INSTANCE);
        } else {
            INSTANCE.setCurrentQuestion(INSTANCE.questions.get(currentPosition));
            GlobalObserverList.getInstance().firePropertyChange("Update question", currentPosition, INSTANCE);
        }
    }

    public void lastQuestion() {
        if (currentPosition != 0) {
            currentPosition = currentPosition - 1;
            INSTANCE.setCurrentQuestion(INSTANCE.questions.get(currentPosition));
            GlobalObserverList.getInstance().firePropertyChange("Update question", currentPosition, INSTANCE);
        }

    }
}
